'use strict'

define(['jquery', 'bootstrap', 'select2', 'validate', 'validate.en', 'shiftform'], function($, validate) {
  
  return {
    
    init: function() {
      this.initPage();
      this.sign();
      this.batchSign();
      this.reject();
      this.print();
    },
    
    initPage: function() {
      
      var that = this;
      
      $('a.vehicle-batch-sign').addClass('disabled');
      $('a.vehicle-export').addClass('disabled');
      
      $(document).on('click', '#selectAll', function(e) {
        $('input[name="vehicleId"]').prop('checked', $('#selectAll').is(':checked'));
      });
      
      $(document).on('change', '#selectAll, input[name="vehicleId"]', function(e) {
        var vehicleIds = $('input[name="vehicleId"]:checked').val();
        if (vehicleIds) {
          $('a.vehicle-batch-sign').removeClass('disabled');
          $('a.vehicle-export').removeClass('disabled');
        } else {
          $('a.vehicle-batch-sign').addClass('disabled');
          $('a.vehicle-export').addClass('disabled');
        }
      });
      
    },
    
    sign: function() {
      $(document).on('shown.bs.modal', '#sign', function(e) {
        var $form = $(e.currentTarget).find('form');
        $form.shiftform();
      });
    },
    
    batchSign: function() {
      $(document).on('shown.bs.modal', '#batch-sign', function(e) {
        
        var $form = $(e.currentTarget).find("form");
        $("input[name='vehicleId']:checked").each(function(){
          var inputHtml = "<input type='hidden' name='vehicleIds' value='"+$(this).val()+"' />";
          $form.append(inputHtml);
        })
        
        $form.shiftform();
        
      });
    },
    
    reject: function() {
      $(document).on('shown.bs.modal', '#reject', function(e) {
        var $form = $(e.currentTarget).find('form');
        $form.shiftform();
      });
    },
    
    print: function() {
      
      $(document).on('shown.bs.modal', ' #export', function(e) {
        var $form = $(e.currentTarget).find("form");
        $("input[name='vehicleId']:checked").each(function(){
          var inputHtml = "<input type='hidden' name='vehicleIds' value='"+$(this).val()+"' />";
          $form.append(inputHtml);
        })
      });
      
    }
    
  }
})